<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminCateController extends Controller
{
    /**
    * 分类的添加页面
    */
    public function getAdd()
    {
        //获取数据库中的表单数据
        $res = DB::table('project_type')->get();
        //遍历
         foreach ($res as $k => $v) {

           //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-2;    
           //根据逗号的数量进行重复
           $v->typename = str_repeat('|--', $len).$v->typename;

            
        }

        //获取数据
       return view('admin/cate/add',['row'=>$res]);
    }

    /**
    *   分类数据的添加方法
    */
    public function postInsert(Request $request)
    {   
        //获取输出的数据
        $res = $request->except('_token');
       
        //获取取顶级pid   0
       $pid = $request->input('pid');
        //判断
        if($pid == '0'){

            //path获取路径
            $res['path'] = '0'.',';
        }else{

            $res = $request->except('_token');

            $row = DB::table('project_type')->where('id',$pid)->first();
            
            //拼接path路径
            $res['path']  = $row->path.$row->id.',';
            //var_dump($res);die;
        }
           
            //插入数据表
            $row = DB::table('project_type')->insert($res);
            //var_dump($row);
            //判断$row
            if($row){

                    return redirect('/Admin/admincate/index')->with('success','添加成功');
            }else{

                return back()->with('error','添加失败');
            }
    }

    /**
    *   分类数据的显示页面
    */
    public function getIndex(Request $request)
    {   
        //获取数据
      $res = DB::table('project_type')->

        select(DB::raw('*,concat(path,id) as pathes'))->

        orderby('pathes')->

        where('typename','like','%'.$request->input('search').'%')->

        paginate($request->input('num',5));
        //var_dump($res);
        foreach ($res as $k => $v) {

           //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-2;    
           //根据逗号的数量进行重复
           $v->typename = str_repeat('|--', $len).$v->typename;

            
        }


         return view('admin/cate/index',['row'=>$res,'request'=>$request->all()]); 
    }

    /**
    *   获取分类修改的页面的数据
    */
    public function getEdit(Request $request)
    {   
        //获取id
        $id = $request->input('id');
        //var_dump($id);die;
        //获取点击的id
        $res = DB::table('project_type')->where('id',$id)->first();
        //var_dump($res);die;
        //获取全部的数据
        $row = DB::table('project_type')->
        select(DB::raw('*,concat(path,",",id) as pathes'))->
        orderby('pathes')->
        get();
        //var_dump($row);
        

        foreach ($row as $k => $v) {

           //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-2;    
           //根据逗号的数量进行重复
           $v->typename = str_repeat('|--', $len).$v->typename;

            
        }


       return view('admin/cate/edit',['list'=>$res,'row'=>$row]);
    }

    /**
    *  分类数据修改的方法
    */
    public function postUpdate(Request $request)
    {   
        //获取id
        $id = $request ->input('id');
        
        //var_dump($id);die;
        //获取数据
       $res = $request->except('_token','id');

       //修改数据
       $row = DB::table('project_type')->where('id','=',$id)->update($res);
     
        if($row){

            return redirect('/Admin/admincate/index')->with('success','修改成功');
        }else{

            return back()->with('error','修改失败');
        }
      
    }

    /**
    *   分类数据的删除方法
    */
    public function getDelete(Request $request)
    {   
        //获取id
        $id = $request->input('id');
        //获取id值
        $res = DB::table('project_type')->where('id',$id)->first();
        //获取路径
        $path = $res->path.','.$res->id;
        //模糊查询
         $row = DB::table('project_type')->where('path','like',$path.'%')->delete();
         // 删除自身
         $self = DB::table('project_type')->where('id',$id)->delete();
         //var_dump($self);
        
        if($self){

            return redirect('/Admin/admincate/index')->with('success','修改成功');
        }else{

            return back()->with('error','修改失败');
        }

    
    }

    /**
    *   前台无限分类递归获取数据
    */
    public function getCateByMessage($pid)
    {
        //获取顶级分类信息
        $res = DB::table('project_type')->where('pid',$pid)->get();
        //var_dump($res);die; 

        //定义空数组
        $arr = [];
        //遍历
        foreach ($res as $k => $v) {

            $v->cate = $this->getCateByMessage($v->id);
            //将遍历信息存入数组
            $arr[] = $v;  
        }

        return $arr;
    }
    //调用递归数据
    public function getCates()
    {
        $row = $this->getCateByMessage(0); 
       // dd($row);
    }
}
    